package com.hao.vod.controller;

import com.hao.commonutils.R;
import com.hao.vod.service.VodService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author chenwenhao
 * @version 1.0.0
 * @ClassName VodController.java
 * @Description TODO
 * @createTime 2020/12/12,17:13
 */
@CrossOrigin
@RestController
@RequestMapping("/eduvod/video")
public class VodController {

    @Autowired
    private VodService vodService;

    /**
     * 阿里上传视频
     *
     * @param file 文件
     * @return {@link R}
     */
    @PostMapping("uploadAliVideo")
    public R uploadAliVideo(MultipartFile file) {
        String videoId = vodService.uploadAliVideo(file);
        return R.ok().data("videoId", videoId);
    }

    /**
     * 删除阿里视频
     *
     * @param videoId 视频id
     * @return {@link R}
     */
    @DeleteMapping("{videoId}")
    public R removeAliVideo(@PathVariable String videoId) {
        vodService.removeAliVideo(videoId);
        return R.ok();
    }

    /**
     * 删除多个阿里视频
     *
     * @return {@link R}
     */
    @DeleteMapping("deleteAliVideos")
    public R deleteAliVideos(@RequestParam("videoIds") List<String> videoIds) {
        String ids = StringUtils.join(videoIds.toArray(), ",");
        vodService.removeAliVideo(ids);
        return R.ok();
    }

    /**
     * 获得视频播放凭证
     *
     * @param id id
     * @return {@link R}
     */
    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id) {
        String auth = vodService.getPlayAuth(id);
        return R.ok().data("playAuth", auth);
    }
}
